const express = require("express")
const router = express.Router()
const { db,genid } = require("../database/adapter")
const {v4:uuidv4} = require("uuid")

router.post("/register",async (req,res) => {
    let {account,password} = req.body;
    
    //查询是否存在账号
    let {err,rows} = await db.async.all("select * from admin where account = ? ",[account])

    if (err == null && rows.length === 0 ){

        const cmd_register = "INSERT INTO `admin` (id,account,password) VALUES (?,?,?)"
        console.log(account,password)
        let {err1,rows1} = await db.async.all(cmd_register,[ genid.NextId(),account,password])
        console.log(err1,rows1)

        
        res.send({
            code:200,
            msg:"注册成功",
        })
    }else if(rows.length != 0){
        res.send({
            code:501,
            msg:"账号已存在"
        })
    }else{
        res.send({
            code:500,
            msg:"注册失败"
        })
    }

})

module.exports = router
